
import java.sql.*;

// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`，
// 然后按 Enter 键。现在，您可以在代码中看到空格字符。
class DbConn{
    public static Connection GetConn(){
        String url = "jdbc:mysql://localhost:3306/my_new_database?useSSL=false&serverTimezone=UTC";
        String user = "root"; // 替换为你的MySQL用户名
        String password = "CST200212"; // 替换为你的MySQL密码
        try {
            return DriverManager.getConnection(url,user,password);
        } catch (SQLException e)
        {
            throw new RuntimeException(e);

        }
    }
}
class Add{
    public static void InsertData() throws SQLException {
        String sql="INSERT INTO first_table (item_name, Value) VALUES (?, ?)";
        Connection conn=DbConn.GetConn();
        conn.prepareStatement(sql);
        PreparedStatement preparedStatement= conn.prepareStatement(sql);
        preparedStatement.setString(1,"diannao");
        preparedStatement.setString(2,"12300");
        preparedStatement.executeUpdate();
    }
}
class  Updata{
    public static void upData(int id,String price) throws SQLException {
        String sql = "UPDATE first_table SET  Value = ? WHERE Id = ?";
        Connection conn=DbConn.GetConn();
        conn.prepareStatement(sql);
        PreparedStatement preparedStatement= conn.prepareStatement(sql);
        preparedStatement.setString(1,price);
        preparedStatement.setInt(2,id);
        preparedStatement.executeUpdate();
    }
}
class  Deleate{
    public static void deleateData(int id) throws SQLException {
        String sql = "DELETE FROM first_table WHERE Id = ?";
        Connection conn=DbConn.GetConn();
        conn.prepareStatement(sql);
        PreparedStatement preparedStatement= conn.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.executeUpdate();
    }
}

class  SelectAll{
    public static void  selectData(int id) throws SQLException {
        String sql = "SELECT * FROM first_table where Id = ?";
        Connection conn=DbConn.GetConn();
        conn.prepareStatement(sql);
        PreparedStatement preparedStatement= conn.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        ResultSet rs = preparedStatement.executeQuery();
        while (rs.next()){

            String itemName = rs.getString("item_name");
            String value = rs.getString("Value");
            System.out.println("ID: " + id + ", Item Name: " + itemName + ", Value: " + value);
        }
        System.out.println(rs);
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(DbConn.GetConn());
        try {
            Add.InsertData();
//            Updata.upData(15,"9800");
//            SelectAll.selectData(15);
//            Deleate.deleateData(15);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}